﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RPSImporter.Models;
using RPSImporter.Infrastructure;
using RPSImporter.Models.Importers;
using RPSImporter.Services;
using RPSImporter.Models.Settings;

namespace RPSImporter.Controllers
{
   public class HomeController : BaseController
   {
      //
      // GET: /Home/
      public ActionResult Index()
      {
         return this.View();
      }

      public ViewResult ImportFile()
      {
         return this.View();
      }

      public ViewResult ImportFtpView()
      {
         return View();
      }

      public ViewResult ImportFtp()
      {
         FtpSvc.Instance.Import();
         this.ViewBag.Error = FtpSvc.Instance.Error;
         return this.View();
      }

      public ViewResult ViewSettings()
      {
         return this.View(DatabaseSvc.Instance.Settings.GeneralSettings.FirstOrDefault());
      }

      [HttpPost]
      public ActionResult ViewSettings(GeneralSettings settings,
                                       PersonFileSettings personFileSettings,
                                       CompanyFileSettings companyFileSettings,
                                       EnterpriseFileSettings enterpriseFileSettings,
                                       HierarchyFileSettings hierarchyFileSettings,
                                       HoursHierarchyFileSettings hoursHierarchyFileSettings,
                                       HoursPersonFileSettings hoursPersonFileSettings)
      {
         if (ModelState.IsValid)
         {
             DatabaseSvc.Instance.ReinitSettings();
            DatabaseSvc.Instance.Settings.Entry(settings).State = System.Data.EntityState.Modified;
            DatabaseSvc.Instance.Settings.Entry(personFileSettings).State = System.Data.EntityState.Modified;
            DatabaseSvc.Instance.Settings.Entry(companyFileSettings).State = System.Data.EntityState.Modified;
            DatabaseSvc.Instance.Settings.Entry(enterpriseFileSettings).State = System.Data.EntityState.Modified;
            DatabaseSvc.Instance.Settings.Entry(hierarchyFileSettings).State = System.Data.EntityState.Modified;
            DatabaseSvc.Instance.Settings.Entry(hoursHierarchyFileSettings).State = System.Data.EntityState.Modified;
            DatabaseSvc.Instance.Settings.Entry(hoursPersonFileSettings).State = System.Data.EntityState.Modified;
            DatabaseSvc.Instance.Settings.SaveChanges();
            return RedirectToAction("Index", "Home");
         }
         return View(settings);
      }
   }
}